package com.arvin.controller;

import com.arvin.pojo.Log;
import com.arvin.pojo.Notice;
import com.arvin.pojo.PageBean;
import com.arvin.pojo.Result;
import com.arvin.service.LogService;
import com.arvin.service.NoticeService;
import com.arvin.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.Not;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "公告相关接口",description = "公告相关接口")
@RequestMapping("/notice")
@Slf4j
public class NoticeController {

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private LogService logService;


    /**
     * 分页查询公告信息
     * @param noticeTitle
     * @param noticeType
     * @param pageSize
     * @return
     */
    @GetMapping("/getInfo")
    @Operation(summary = "获取公告信息")
    public Result<PageBean> getNoticeInfo(String noticeTitle,String noticeType,
                                          String noticeState, Integer page,
                                          Integer pageSize){
        PageBean pageBean = noticeService.getNoticeInfo(noticeTitle, noticeType,
                 noticeState,page,pageSize);
        log.info("获取公告信息{}",pageBean);
        return Result.success(pageBean);
    }

    /**
     * 获取全部公告信息
     * @param noticeState
     * @return
     */
    @GetMapping("/getAllNotice")
    @Operation(summary = "获取全部公告信息")
    public Result<List<Notice>> getAllNotice(String noticeState){
        List<Notice> noticeList = noticeService.getAllNotice(noticeState);
        log.info("获取全部公告信息{}",noticeList);
        return Result.success(noticeList);
    }

    /**
     * 根据id查询公告
     * @param noticeId
     * @return
     */
    @Operation(summary = "根据id查询公告",description = "根据id查询公告")
    @GetMapping("/getInfo/{noticeId}")
    public Result<Notice> getNoticeInfoById(@PathVariable Integer noticeId){
        Notice notice = noticeService.getNoticeInfoById(noticeId);
        return Result.success(notice);
    }

    /**
     * 发布公告
     * @param notice
     * @return
     */
    @Operation(summary = "发布公告",description = "发布公告")
    @PostMapping("/publishNotice")
    public Result<Notice> publishNotice(@RequestBody Notice notice){
        log.info("公告信息{}", notice);
        noticeService.publishNotice(notice);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("公告");
        log.setLogOperate("管理员" + username + "新增公告信息：" + notice);
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);
        return Result.success(notice);
    }

    /**
     * 修改公告
     * @param notice
     * @return
     */
    @Operation(summary = "修改公告" ,description = "修改公告")
    @PutMapping("/updatePublishNotice")
    public Result updatePublishNotice(@RequestBody Notice notice){
        log.info("修改公告信息{}", notice);
        noticeService.updatePublishNotice(notice);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("公告");
        log.setLogOperate("管理员" + username + "修改公告信息：" + notice);
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);
        return Result.success();
    }

    /**
     * 删除公告
     * @param noticeId
     * @return
     */
    @Operation(summary = "删除公告",description = "根据公告id删除")
    @DeleteMapping("/noticeDelete/{noticeId}")
    public Result deleteNotice(@PathVariable("noticeId") Integer noticeId){
        Notice notice = noticeService.getNoticeInfoById(noticeId);
        noticeService.deleteNotice(noticeId);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("公告");
        log.setLogOperate("管理员" + username + "删除了公告信息：" + notice);
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 更新公告状态
     * @param notice
     * @return
     */
    @Operation(summary = "更新公告状态",description = "更新公告状态")
    @PutMapping("/updateState")
    public Result updateNoticeState(@RequestBody Notice notice){
        noticeService.updateNoticeState(notice);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("公告");
        log.setLogOperate("管理员" + username + "修改了公告状态：" + notice);
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }
}
